﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using RuleCode.Table;
using RuleCode.Common;
using System.Reflection;
using System.Drawing;
using RuleCode.ObjectTree;
namespace ERPWebDemos.Tools
{
    /// <summary>
    /// 参数： ID ,EDTType
    /// </summary>
    public partial class EDTLookUpForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridView("");
            }
            
        }

        protected string EDTFieldName = string.Empty;

        #region 初始化
        protected void BindGridView(string SortingString)
        {
            string edtTypeStr = Request["edttype"];
            if (!String.IsNullOrEmpty(edtTypeStr))
            {
                Assembly ass = RuleCode.Common.AssemblyClass.MyAssembly;
                ExtendDataType edt = ass.CreateInstance("RuleCode.ExtendType." + edtTypeStr) as ExtendDataType;
                if (edt.Relations.Count > 0)
                {
                    RuleCode.Common.Table table = 
                        edt.Relations[0].TableType.GetConstructor(System.Type.EmptyTypes).Invoke(null) as RuleCode.Common.Table;
                    string fieldName = edt.Relations[0].FieldName;
                    EDTFieldName = fieldName;
                    MainGridView.DataKeyNames = new string[] { fieldName };
#region 添加合适的列用于显示
                    string filter = "";
                    TableFieldType tft = (TableFieldType)table[fieldName];
                    addcolumn(tft);
                    filter += getfieldfilter(tft);
                    AOTFieldGroup afg = new AOTFieldGroup();
                    Int32 i = 0;
                    foreach (AOTFieldGroup item in table.AOTFieldGroups)
                    {
                        if (item.Name == "AutoLookup" && item.FieldGroup.Count > 0)
                        {
                            afg = item;
                        }
                        if (item.Name == "OverView" && item.FieldGroup.Count > 0 && afg.FieldGroup.Count < 1)
                        {
                            afg = item;
                        }
                    }
                    if (afg.FieldGroup.Count > 0)
                    {
                        foreach (AOTField item in afg.FieldGroup)
                        {
                            if (item.FieldName != fieldName)
                            {
                                tft = (TableFieldType)table[item.FieldName];
                                addcolumn(tft);
                                filter += getfieldfilter(tft);

                            }
                        }
                    }
                    else
                    {
                        foreach (string item in table.AllFields)
                        {
                            if (i >= 3)
                            {
                                break;
                            }

                            if (item != fieldName && item != "RECID" && item != "DATAAREAID")
                            {
                                tft = (TableFieldType)table[item];
                                addcolumn(tft);
                                filter += getfieldfilter(tft);
                                i++;
                            }
                        }
                    }
                    if (filter != "")
                    { 
                        filter = " ( 1=2 " + filter + " )";
                    }
#endregion



                    table.FilterString = filter;
                    table.SortingString = DropDownList1.ToolTip;
                    table.PageEnabled = true;
                    table.PageSize = 20;

                    if (SortingString == "1")
                    {
                        table.Fetch(RuleCode.zfunction.str2int(DropDownList1.SelectedValue));
                    }
                    else
                    { 
                        table.Fetch(RuleCode.zfunction.str2int(DropDownList1.SelectedValue));    
                        DropDownList1.Items.Clear();
                        for (int ii = 0; ii < table.PageCount; ii++)
                        {
                            DropDownList1.Items.Add(ii.ToString());
                        }                
                    }



                    



                    //MainWebPager.DataSource = table.DT;
                    //MainWebPager.DataBind();
                    MainGridView.DataSource = table.DT;
                    MainGridView.DataBind();



                    //RuleCode.Common.Table table = RuleCode.Common.Table.Find("", edt.Relations[0].TableType);
                    ////RuleCode.Common.Table table = edt.Relations[0].TableType.GetConstructor(System.Type.EmptyTypes).Invoke(null) as RuleCode.Common.Table ;
                    ////table.CurPage = MainWebPager.CurrentPageIndex;
                    

                    //if (!IsPostBack)
                    //{
                    //    string fieldName = edt.Relations[0].FieldName;
                    //    EDTFieldName = fieldName;
                    //    MainGridView.DataKeyNames = new string[] { fieldName };

                    //    MainGridView.AutoGenerateColumns = false;
                    //    BoundField edtColumn = new BoundField();
                    //    edtColumn.HeaderText = EDTFieldName;
                    //    edtColumn.DataField = EDTFieldName;
                    //    edtColumn.HeaderStyle.BackColor = Color.LightBlue;
                    //    edtColumn.ItemStyle.BackColor = Color.LightBlue ;
                    //    MainGridView.Columns.Add(edtColumn);

                    //    foreach (string caption in table.AllFields )
                    //    {
                    //        if (caption == EDTFieldName)
                    //            continue;
                    //        BoundField column = new BoundField();
                    //        column.HeaderText = caption;
                    //        column.DataField = caption;
                    //        MainGridView.Columns.Add(column);
                    //    }
                    //}

                    //MainWebPager.DataSource = table.MyTable;
                    //MainWebPager.DataBind();
                    
                     
                }                       
            }
        }
        private string getfieldfilter(TableFieldType f)
        {
            string filter = "";
            if (TextBox1.Text.Trim() != "")
            {
                if (f is TString)
                {
                    filter += " or " + f.BaseProperty.Name + " like '%" + TextBox1.Text.Trim() + "%' ";
                }
            }
            return filter;
        }
        private void addcolumn(TableFieldType _tft)
        {
            if (IsPostBack)
            {
                return;
            }
            BoundField bf = new BoundField();
            if (_tft.Text != "")
            {
                bf.HeaderText = _tft.Text;
            }
            else
            {
                bf.HeaderText = _tft.BaseProperty.Name;
            }
            bf.DataField = _tft.BaseProperty.Name;
            bf.ReadOnly = true;
            bf.SortExpression = _tft.BaseProperty.Name;
            MainGridView.Columns.Add(bf);

        }
        #endregion

        #region 绑定事件
        protected void MainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HtmlInputHidden inputText = e.Row.FindControl("BindFieldText") as HtmlInputHidden;
                if (inputText != null)
                {
                    inputText.Value = MainGridView.DataKeys[e.Row.RowIndex].Value.ToString();
                }      
            }
        }

        ///// <summary>
        ///// 页面当前页改变事件
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void MainWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        //{
        //    MainWebPager.CurrentPageIndex = e.NewPageIndex;
        //    BindGridView();
        //}
        #endregion

        protected void MainGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            string SortingString = "";
            if (e.SortExpression != "")
            {
                if (TextBox1.ToolTip == "")
                {
                    TextBox1.ToolTip = "1";
                    e.SortDirection = SortDirection.Ascending;
                }
                else
                {
                    TextBox1.ToolTip = "";
                    e.SortDirection = SortDirection.Descending;
                }
                if (e.SortDirection == SortDirection.Ascending)
                {
                    SortingString = " order by " + e.SortExpression + " asc";
                }
                else
                {
                    SortingString = " order by " + e.SortExpression + " desc";
                }
            }
            DropDownList1.ToolTip = SortingString;
            BindGridView("");
        }

        protected void MainGridView_Sorted(object sender, EventArgs e)
        {
            //
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindGridView("");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridView("1");
        }

        protected void Button_prior_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex != 0)
            {
                DropDownList1.SelectedIndex = DropDownList1.SelectedIndex - 1;
            }
            BindGridView("1");
        }

        protected void Button_next_Click(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex != DropDownList1.Items.Count-1)
            {
                DropDownList1.SelectedIndex = DropDownList1.SelectedIndex + 1;
            }
            BindGridView("1");
        }

        
    }
}
